const net = require('net'),
      func = require('./common.js');
var RES,_GET,_POST,h,tool;
exports.main = (req,res) =>{
    tool = require(req.bind.dir+'/module/emma.js');
    var HTML = tool.require('html');
    _GET = req.bind.__GET,
    _POST = req.bind.__POST,
    RES = res; 
    h = new HTML();
    h.isMobile(req,tool);
    h.load({'title':'NET || '+Math.random()*100000,'css':'/node/public/css/net'});
    //req.setEncoding('utf-8');//无效相比=  h.appHead('<meta charset="utf-8">');
    h.appHead('<meta charset="utf-8">');
    var html = '';
    html += func.navlist(func.udata(req.url,'app'));
    html += connform();
    h.appBody(html);
    res.write(h.getHtml());
    //  清除函数缓存
    func.clear(__dirname+'/common.js');
}
function connform(){
    var data = _POST;
    if(tool.empty(data)) data = {};
    var html = '<form method="post">';
    data.port = data.port || 80;
    data.url = data.url || '';
    if(data.url){
        conn({'port':data.port,'host':data.url});
    }
    html += '<br><lable>域名</lable>：'+h.text({'name':'url','placeholder':'域名','value':data.url,'required':'required','class':'url'});
    html += '<br><lable>端口号</lable>：'+h.text({'name':'port','placeholder':'端口号','value':data.port,'required':'required'});
    html += '<br>'+h.text({'type':'submit','value':'连接'});
    html += '</form>';
    return html;
}
function conn(opt){
    const client = net.connect(opt, () => {
        RES.write('connected to server!');
        RES.write('world!\r\n');
    });
    client.on('data', (data) => {
        RES.write(data.toString());
        client.end();
    });
    client.on('end', () => {
        RES.write('disconnected from server');
    });
}